<?php
import( 'de.ceus-media.framework.krypton.core.DefinitionView' );
import( 'de.ceus-media.framework.krypton.view.component.List' );
import( 'classes.logic.User' );
import( 'classes.logic.Role' );
import( 'classes.logic.Right' );
import( 'classes.collection.User' );
/**
 *	User Views.
 *	@package		mv2.view.administration
 *	@extends		Framework_Krypton_Core_DefinitionView
 *	@uses			Framework_Krypton_View_Component_List
 *	@uses			Logic_User
 *	@uses			Logic_Role
 *	@uses			Collection_User
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
/**
 *	User Views.
 *	@package		mv2.view.administration
 *	@extends		Framework_Krypton_Core_DefinitionView
 *	@uses			Framework_Krypton_View_Component_List
 *	@uses			Logic_User
 *	@uses			Logic_Role
 *	@uses			Collection_User
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
class View_Administration_User extends Framework_Krypton_Core_DefinitionView
{
	/**
	 *	Builds Views.
	 *	@access		public
	 *	@return		string
	 */
	public function buildContent()
	{
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );
		$content	= "";
//		if( $auth->isAuthenticated() )
//		{
			if( $request->has( 'add' ) )
			{
				$content	.= $this->buildContentAdd();
#				$content	.= $this->buildContentRoles( 'add' );
			}
			else if( $request->has( 'userId' ) )
			{
				$content	.= $this->buildContentEdit();
				$content	.= $this->buildContentStatus();
				$content	.= $this->buildContentRoles();
				$content	.= $this->buildContentPages();
			}
			else
			{
				$content	.= $this->buildContentFilter();
				$content	.= $this->buildContentList();
			}
//		}
		return $content;
	}

	/**
	 *	Builds edit User View.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentAdd()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['user']['addUser'];

		$sources	= array();
		$ui	= $this->buildForm( 'administration/user', 'addUser', 'user', 'addUser', array(), $sources );
		$button_cancel	= $this->html->LinkButton( "?link=".$request->get( 'link' ), $words['button_cancel'], 'but cancel' );
		$button_add		= $this->html->Button( 'addUser', $words['button_add'], 'but add' );
		$ui['field_button_cancel']	= $this->html->Field( '', $button_cancel );
		$ui['field_button_add']		= $this->html->Field( '', $button_add);
		$ui['form']			= $this->html->Form( 'addUser', "?link=".$request->get( 'link' )."&add" );
		$ui['colgroup']		= $this->html->ColumnGroup( $words['colgroup'] );
		$ui['caption']		= $this->html->TableCaption( $words['caption'], 'add' );
		$ui['heading']		= $words['heading'];

		return $this->loadTemplate( 'administration.user.add', $ui );
	}

	/**
	 *	Builds edit User View.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentEdit()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['user']['editUser'];
		$userId		= $request->get( 'userId' );

		$user		= new Model_User( $userId );
		$data		= $user->get();

		$sources	= array();
		$ui	= $this->buildForm( 'administration/user', 'editUser', 'user', 'editUser', $data, $sources );
		$button_cancel	= $this->html->LinkButton( "?link=".$request->get( 'link' ), $words['button_cancel'], 'but cancel' );
		$button_edit	= $this->html->Button( 'editUser', $words['button_edit'], 'but edit' );
		$ui['field_button_cancel']	= $this->html->Field( '', $button_cancel );
		$ui['field_button_edit']	= $this->html->Field( '', $button_edit);
		$ui['form']			= $this->html->Form( 'editUser', "?link=".$request->get( 'link' )."&userId=".$userId );
		$ui['colgroup']		= $this->html->ColumnGroup( $words['colgroup'] );
		$ui['caption']		= $this->html->TableCaption( $words['caption'], 'edit' );
		$ui['heading']		= $words['heading'];

		return $this->loadTemplate( 'administration.user.edit', $ui );
	}

	/**
	 *	Builds edit User View.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentFilter()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$session	= $this->registry->get( 'session' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['user']['filterUsers'];

		$user	= new Model_User();
		$fields	= $user->getColumns();
		$opt_order	= array( "" => $words['order_none'] );
		foreach( $fields as $field )
			if( isset( $words['order_'.$field] ) )
				$opt_order[$field]	= $words['order_'.$field];
		if( $select = $session->get( 'administration_users_order' ) )
			$opt_order['_selected']	= $select;
		$values	= array(
			'title'		=> $session->get( 'administration_users_title' ),
			'direction'	=> $session->get( 'administration_users_direction' ),
			'limit'		=> $session->get( 'administration_users_limit' ),
			);
		$sources	= array(
			'opt_order'	=> $opt_order,
			);

		$ui	= $this->buildForm( 'administration/user', 'filterUsers', 'user', 'filterUsers', $values, $sources );

		$input_button_filter		= $this->html->Button( 'filterUsers', $words['button_filter'], 'but filter' );
		$input_button_reset			= $this->html->LinkButton( "?link=".$request->get( 'link' )."&resetFilters", $words['button_reset'], 'but reset' );
		$ui['field_button_filter']	= $this->html->Field( 'filterUsers', $input_button_filter );
		$ui['field_button_reset']	= $this->html->Field( 'resetFilters', $input_button_reset );
		$ui['form']		= $this->html->Form( 'filterUsers', "?link=".$request->get( 'link' ) );
		$ui['caption']	= $this->html->TableCaption( $words['caption'], 'filter' );
		$ui['colgroup']	= $this->html->ColumnGroup( $words['colgroup'] );
		$ui['heading']	= $words['heading'];

		return $this->loadTemplate( 'administration.user.filter', $ui );
	}

	/**
	 *	Builds edit User View.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentList()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$session	= $this->registry->get( 'session' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['user']['listUsers'];

		$options	= array(
			'coverage'		=> $config['layout.list_coverage'],
			'key_offset'	=> "offset",
			);
		$heads	= array(
			$words['head_title'],
			$words['head_roles'],
			"", "", "", "", "", "", "",
			);

		$actions	= array();
		$list	= new Framework_Krypton_View_Component_List( 'Collection_User' );
		$list->setActionForm( 'listUsers' );
		$list->setActions( $actions );
		$list->setCaption( $words['caption'] );
		$list->setColumnGroup( $words['colgroup'] );
		$list->setTemplates( 'administration.user.list', 'administration.user.listitem', 'paging' );
		$list->setLimitKey( 'administration_users_limit' );
		$list->setOffsetKey( 'administration_users_offset' );
		$list->setHeads( $heads );
		$list->setPagingOptions( $options );
		$list->setTransformer( $this, 'transformListEntry' );

		$list->collectUsers();
		if( $session->get( 'administration_users_order' ) )
			$list->orderBy( $session->get( 'administration_users_order' ), $session->get( 'administration_users_direction' ) );
		if( $session->get( 'administration_users_username' ) )
			$list->withUsernameLike( $session->get( 'administration_users_username' ) );
		if( $session->get( 'administration_users_email' ) )
			$list->withEmailLike( $session->get( 'administration_users_email' ) );
		$list	= $list->getList();

		$ui['list']		= $list;
		$ui['heading']	= $words['heading'];
		$ui['link_add']	= $this->html->LinkButton( "?link=".$request->get( 'link' )."&add", $words['link_add'], 'but add' );
		return $this->loadTemplate( 'administration.user.users', $ui );
	}

	private function buildContentPages()
	{
		$request	= $this->registry->get( 'request' );
		$controller	= $this->registry->get( 'controller' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['user']['viewPages'];

		$userId		= $request->get( 'userId' );
		$userRoles	= array();
		$roles		= Logic_Role::getUserRoles( FALSE, $userId );
		foreach( array_values( $roles ) as $roleTitle )
			$userRoles[]	= strtolower( str_replace( " ", "_", $roleTitle ) );
        $userRoles	= array_merge( $userRoles, array( 'inside', 'public' ) );
		$mainPages	= $controller->getPages( 'main' );

		$i		= 0;
		$list	= array();
		$pages		= array();
		$scope 	= 'links_main';
		foreach( $mainPages as $pageId => $pageData )
		{
			if( isset( $pageData['parent'] ) )
			{
				$parent	= $pageData['parent'];
		#		$scope	= "links_".$pageData['scope'];
				$label	= $pageId;
				if( isset( $this->words['main'][$scope][$pageId] ) )
					$label	= strip_tags( $this->words['main'][$scope][$pageId] );
				if( !array_intersect( $pageData['roles'], $userRoles ) )
					$label	= "<strike>".$label."</strike>";
				$item	= array();
				$roles	= array();
				$item['label']	= $label;
				$item	= $this->loadTemplate( 'administration.user.pages.listitem', $item );
				$pages[$parent]['pages'][]	= $this->html->ListItem( $item, 2, array( 'class' => 'sub' ) );
			}
			else
			{
				$pages[$pageId]['scope']	= $pageData['scope'];
				$pages[$pageId]['roles']	= $pageData['roles'];
			}
		}
		$list	= array();
		foreach( $pages as $mainPageId => $mainPageData )
		{
			$label	= $mainPageId;
			if( isset( $this->words['main'][$scope][$mainPageId] ) )
				$label	= strip_tags( $this->words['main'][$scope][$mainPageId] );
			$scope	= "links_".$mainPageData['scope'];
			$item	= array();
			$roles	= array();
			if( !array_intersect( $mainPageData['roles'], $userRoles ) )
				$label	= "<strike>".$label."</strike>";
			$item['pages']	= isset( $mainPageData['pages'] ) ? $this->html->unorderedList( $mainPageData['pages'] ) : "";
			$item['label']	= $label;
			$item	= $this->loadTemplate( 'administration.user.pages.listitem', $item );
			$list[]	= $this->html->ListItem( $item, 1, array( 'class' => 'main' ) );
		}
		$ui['list']		= $this->html->unorderedList( $list, 0, array( 'class' => 'pages' ) );

		$ui['caption']		= $this->html->TableCaption( $words['caption'], 'edit' );
		$ui['heading']		= $words['heading'];
		return $this->loadTemplate( 'administration.user.pages.list', $ui );
	}

	/**
	 *	Builds Locations View.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentRoles( $page = 'edit')
	{
		$logic		= new Logic_User();
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$session	= $this->registry->get( 'session' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['user']['editUserRoles'];
		$userId		= $request->get( 'userId' );

		$roles		= Logic_Role::getUserRoles( FALSE, $userId );
		$assigned	= array();
		if( $page == 'edit' )
			foreach( $roles as $roleId => $roleTitle )
				$assigned[]	= $roleId;
		else
			$assigned = $session->get( 'add_user_roles' );
		$all		= Logic_Role::getRoles( FALSE );

		$has	= array();
		$more	= array();
		foreach( $all as $roleId => $roleTitle )
		{
			if( in_array( $roleId, $assigned ) )
				$has[$roleId]	= $roleTitle;
			else
				$more[$roleId]	= $roleTitle;
		}
		$sources	= array(
			'opt_remove'	=> $has,
			'opt_add'		=> $more,
			);

		$ui	= $this->buildForm( 'administration/user', 'editUserRoles', 'user', 'editUserRoles', array(), $sources );

		$button_add		= $this->html->Button( 'addUserRoles', $words['button_add'], 'but add' );
		$button_remove	= $this->html->Button( 'removeUserRoles', $words['button_remove'], 'buthot remove' );

		$ui['field_button_add']		= $this->html->Field( '', $button_add );
		$ui['field_button_remove']	= $this->html->Field( '', $button_remove );

		$ui['form']		= $this->html->Form( 'editUserRoles' );
		$ui['caption']		= $this->html->TableCaption( $words['caption'], $page );
		$ui['colgroup']		= $this->html->ColumnGroup( $words['colgroup'] );
		$ui['heading']		= $words['heading'];
		return $this->loadTemplate( 'administration.user.roles', $ui );
	}

	/**
	 *	Builds Status View.
	 *	@access		private
	 *	@return		string
	 */
	private function buildContentStatus()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$session	= $this->registry->get( 'session' );
		$auth		= $this->registry->get( 'auth' );
		$words		= $this->words['user']['editStatus'];
		$userId		= $request->get( 'userId' );

		$logic		= new Logic_User();
		$status		= $logic->getUserStatus( $userId );
		$data['set_status']	= (string) $status;
		$data['status']		= $this->getStatusIcon( $status )." ".$this->words['user']['user_states'][$status];
		$opt_status	= $this->words['user']['user_states'];
		unset( $opt_status[0] );
		$opt_status['_selected']	= $status;
		$sources	= array(
			'opt_status'	=> $opt_status,
			);

		$ui	= $this->buildForm( 'administration/user', 'editStatus', 'user', 'editStatus', $data, $sources );
		$button_edit	= $this->html->Button( 'editStatus', $words['button_edit'], 'buthot edit', $words['button_edit_confirm'] );
		$ui['field_button_edit']	= $this->html->Field( '', $button_edit );
		$ui['form']		= $this->html->Form( 'editStatus', "?link=".$request->get( 'link' )."&userId=".$userId );
		$ui['caption']	= $this->html->TableCaption( $words['caption'], 'edit' );
		$ui['colgroup']	= $this->html->ColumnGroup( $words['colgroup'] );
		$ui['heading']	= $words['heading'];
		return $this->loadTemplate( 'administration.user.status', $ui );
	}

	/**
	 *	Builds Extra Panel.
	 *	@access		public
	 *	@return		string
	 */
	public function buildExtra()
	{
//		return $this->loadContent( 'purchase.market.car.extra.html' );
	}

	private function getStatusIcon( $status )
	{
		$config		= $this->registry->get( 'config' );
		$words		= $this->words['user']['user_states'];
		$path		= $config['paths.images']."icons/silk/";
		switch( $status )
		{
			case -2:	$icon	= "bin_closed";
						break;
			case -1:	$icon	= "lock";
						break;
			case 0:		$icon	= "star";
						break;
			case 1:		$icon	= "accept";
						break;
		}
		$image	= $this->html->Image( $path.$icon.".png", $words[$status] );
		return "<span style='float: left; display: block; margin-right: 4px'>".$image."</span>";
	}

	public function transformListEntry( $entry )
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$words		= $this->words['user']['listUsers'];
		$logic		= new Logic_User();
		$path		= $config['paths.images']."icons/silk/";

		$item['link']		= $this->html->Link( "?link=".$request->get( 'link' )."&userId=".$entry['userId'], $entry['username'] );
		$item['logged']		= $entry['logged'] ? $this->html->Image( $path."key.png", $words['icon_logged']." ".$this->tc->convertToHuman( $entry['logged'], "datetime" ) ) : "";
		$item['status']		= $this->getStatusIcon( $entry['status'], $words['icon_status'] );
		$list	= array();
		$roles	= Logic_Role::getUserRoles( FALSE, $entry['userId'] );
		foreach( $roles as $roleId => $roleTitle )
			$list[]	= $this->html->Link( "?link=roles&roleId=".$roleId, $roleTitle );
		$item['roles']	= implode( ", ", $list );
		return $item;
	}
}
?>
